# Maintainer: Jonathan Armstrong <jda2158@gmail.com>

_realname=flashrom
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.5.1
pkgrel=1
pkgdesc='Utility for detecting, reading, writing, verifying and erasing flash chips (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.flashrom.org/Flashrom'
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-libftdi"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
# Use upstream source tarball for versioninfo.inc file
source=("https://download.flashrom.org/releases/${_realname}-v${pkgver}.tar.xz"{,.asc}
        0001-meson-check-version-script.patch)
sha256sums=('1f934b076ed49eace203655ec249fc7861a6b8e87fe4aef732e47b6e485b6293'
            'SKIP'
            '1fd930d8e76a64f116d971959f26a0b91a96b12235d51ac0fd7f5a09c0a6fbac')
validpgpkeys=('6E6EF9A0BA478006E2776E4CC037BB413134D111') # Stefan Reinauer <stefan.reinauer@coreboot.org>

prepare() {
  cd "${_realname}-v${pkgver}"

  # Workaround: DEF file should be used instead of MAP file
  patch -p1 -i "${srcdir}/0001-meson-check-version-script.patch"
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    CFLAGS+=" -Wno-error=format="
  else
    CFLAGS+=" -Wno-format-security"
  
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson \
      -Dtests=disabled \
      -Dman-pages=enabled \
      -Ddocumentation=disabled \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      ../${_realname}-v${pkgver}

  meson compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  # TODO: check once master branch gets numbered release (i.e., tests/)
  meson test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" meson install

  # To enable usage relative paths for next operations
  cd "${pkgdir}"/${MINGW_PREFIX}

  # Move content to standard dirs
  mv -v sbin/flashrom.exe bin/
  rm -d sbin

  install -Dm644 "${srcdir}/${_realname}-v${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
 
# echo replace USB-UART drivers (if needed) with WinUSB (libusb) using zadig [https://zadig.akeo.ie/]
}
